//
//  XZ_RACController.m
//  Project_Demo
//
//  Created by caoxiaozhu on 2018/7/25.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

#import "XZ_RACController.h"
#import "XZ_LoginView.h"
#import "ViewModel.h"
#import "XZ_SlideColorController.h"
#import "XZ_TestModel.h"

@interface XZ_RACController ()
@property(nonatomic,strong)NSString *loginName;
@property(nonatomic,strong)NSString *value;
@property(nonatomic,strong)RACSignal *signal;
@property(nonatomic,strong)ViewModel *viewModel;

@end

@implementation XZ_RACController

+(void)load{
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"在RAC中实现了didFinishLaunch方法");
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _value = @"1";
    
    //遍历数组
//    [self test1];
    
    //遍历字典
//    [self test2];
    
    //字典转模型
//    [self test3];
    
    ////RAC合并单元信号
//    [self test4];
    
    //filter的过滤应用
//        [self test5];
    
    
    //Map的使用
//    [self test6];
    
    //定时器
//    [self test7];
}

-(void)test1{
    
    //遍历数组
    NSArray *tempArr = @[@"1",@"2",@"3",@"4",@"5"];
    [tempArr.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"x==>%@",x);
    }];
    
}

-(void)test2{
    
    //遍历字典
    NSDictionary *tempDict = @{@"name":@"leon",@"age":@"13",@"sex":@"man"};
    [tempDict.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        //解包元祖
        RACTupleUnpack(NSString *key,NSString *value) = x;
        NSLog(@"key===>%@,value==>%@",key,value);
    }];
}

-(void)test3{
    //字典转模型
    NSMutableArray *flags = [NSMutableArray array];
    NSArray *testArr = @[
@{@"name":@"leon",@"age":@"13",@"sex":@"man",@"phone":@"11111111111"},
@{@"name":@"sisi",@"age":@"18",@"sex":@"woman",@"phone":@"22222222222"}
                         ];
//    [testArr.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
//        XZ_TestModel *model = [XZ_TestModel questionWithDictionary:x];
//        [flags addObject:model];
//
//        [flags.rac_sequence.signal  subscribeNext:^(XZ_TestModel *model) {
//            NSLog(@"name==>%@",model.name);
//            NSLog(@"sex===>%@",model.sex);
//            NSLog(@"age===>%@",model.age);
//            NSLog(@"phone===>%@",model.phone);
//        }];
//    }];
    
    NSArray *tempArr =  [[testArr.rac_sequence map:^id _Nullable(id  _Nullable value) {
        return [XZ_TestModel questionWithDictionary:value];
    }] array];
    NSLog(@"name==>%@",((XZ_TestModel *)tempArr[0]).name);

}

-(void)test4{
    
    RACSignal *r1 =  [self requestData:@"111"];
    RACSignal *r2 =  [self requestData:@"222"];
    RACSignal *r3 =  [self requestData:@"333"];
    
    [[RACSignal combineLatest:@[r1,r2,r3]] subscribeNext:^(RACTuple * _Nullable x) {
        NSLog(@"x==%@",x);
    }];
    
    [[[r1 merge:r2] merge:r3] subscribeNext:^(id  _Nullable x) {
        NSLog(@"xxx==>%@",x);
    }];
    
    [[[r1 concat:r2] concat:r3] subscribeNext:^(id  _Nullable x) {
        NSLog(@"x===========>%@",x);
    }];
    
}

-(RACSignal *)requestData:(NSString *)str{
    RACSignal *requestSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        [subscriber sendNext:str];
        [subscriber sendCompleted];
        
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
    return requestSignal;
}

//filter
-(void)test5{
    RACSignal *signal = [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        [subscriber sendNext:@(18)];
        [subscriber sendNext:@(19)];
        [subscriber sendNext:@(20)];
        [subscriber sendNext:@(15)];
        [subscriber sendNext:@(14)];
        [subscriber sendCompleted];
        return nil;
    }]filter:^BOOL(NSNumber * value) {
        return value.integerValue >= 18;
    }];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"x==>%@",x);
    }];
    
    
}

-(void)test6{
    
    RACSignal *signal = [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"123"];
        [subscriber sendNext:@"323"];
        [subscriber sendCompleted];
        return nil;
    }] map:^id _Nullable(id  _Nullable value) {
        if ([value isEqualToString:@"123"]) {
            return @"223";
        }
        return value;
    }];
    
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"x==>%@",x);
    }];
    
}

-(void)test7{
    RACSignal *signal = [RACSignal interval:3 onScheduler:[RACScheduler mainThreadScheduler]];
    
    RACDisposable *disposable = [[RACDisposable alloc]init];
    
   disposable =  [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"过了3秒得吃药");
       [disposable dispose];
    }];
}




@end
